package mypazar.customer;

import mypazar.agent.PersonAgent;
import mypazar.jenamodel.PazarVocabulary;
import tr.edu.ege.seagent.planner.htn.Action;
import tr.edu.ege.seagent.planner.htn.ExecutionMethod;
import tr.edu.ege.seagent.planner.htn.Outcome;
import tr.edu.ege.seagent.planner.htn.Provision;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * Action class of ACAskAboutProduct.
 */
public class ACAskAboutProduct extends Action {

	/**
	 * SalesmanName provision.
	 */
	@Provision
	private String SalesmanName;

	/**
	 * Returns the value of the SalesmanName provision.
	 */
	public String getSalesmanName() {
		return this.SalesmanName;
	}

	/**
	 * Sets the value of the SalesmanName provision.
	 */
	public void setSalesmanName(String SalesmanName) {
		this.SalesmanName = SalesmanName;
	}

	/**
	 * Product outcome.
	 */
	@Outcome
	private String Product;

	/**
	 * Returns the value of the Product outcome.
	 */
	public String getProduct() {
		return this.Product;
	}

	/**
	 * Sets the value of the Product outcome.
	 */
	public void setProduct(String Product) {
		this.Product = Product;
	}

	/**
	 * Execution method.
	 */
	@ExecutionMethod
	public void execute() {
		Model beliefBase = (Model) getHeapValue(PersonAgent.BELIEF_BASE_ITEM_NAME);
		// set the outcome....
		String PERSON_URI = PazarVocabulary.NS_URI
				+ getAgentIdentifier().toString().substring(0, 7);

		this.setProduct(getProductName(beliefBase, PERSON_URI));

		// add receiver agent...
		this.addReceiverAgent(getSalesmanName() + "@mypazar.com");

	}

	/**
	 * Gets product name from belief base
	 * 
	 * @param beliefBase
	 * @param personURI
	 * @return
	 */
	private String getProductName(Model beliefBase, String personURI) {
		String pName = null;
		Resource resPerson = beliefBase.getResource(personURI);
		StmtIterator iterProduct = resPerson.listProperties(ResourceFactory
				.createProperty(PazarVocabulary.PRODUCT_URI));
		if (iterProduct.hasNext()) {
			Statement stmtProduct = iterProduct.next();
			Resource resProduct = stmtProduct.getObject().asResource();

			pName = resProduct
					.getProperty(
							ResourceFactory
									.createProperty(PazarVocabulary.PRODUCT_NAME_URI))
					.getLiteral().getString();
		}
		return pName;
	}
}
